{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "332126f9-3524-4223-bcc6-24b9818b77b5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from mythic import mythic, mythic_classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abfa531f-5e7a-400b-910c-92a62106ccbe",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mythic_instance = await mythic.login(\n",
    "        username=\"mythic_admin\",\n",
    "        password=\"mythic_password\",\n",
    "        server_ip=\"mythic_nginx\",\n",
    "        server_port=7443,\n",
    "        timeout=-1\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77b89409-31c8-44d8-877f-bec19f261e01",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Register a file and use it with a task ################\n",
    "resp = await mythic.register_file(\n",
    "   mythic=mythic_instance, filename=\"test.txt\", contents=b\"this is a test\"\n",
    ")\n",
    "print(f\"registered file UUID: {resp}\")\n",
    "status = await mythic.issue_task(\n",
    "   mythic=mythic_instance,\n",
    "   command_name=\"upload\",\n",
    "   parameters={\"remote_path\": \"test.js\", \"file_id\": resp},\n",
    "   callback_display_id=1,\n",
    ")\n",
    "print(f\"Issued a task: {status}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d84a9fe1-589e-49ec-a843-4c89a877e684",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Create a new task ################\n",
    "try:\n",
    "    status = await mythic.issue_task(\n",
    "        mythic=mythic_instance,\n",
    "        command_name=\"shell\",\n",
    "        parameters={\"command\": \"whoami\"},\n",
    "        callback_display_id=1,\n",
    "        timeout=10,\n",
    "        wait_for_complete=True,\n",
    "    )\n",
    "    print(f\"Issued a task: {status}\")\n",
    "except Exception as e:\n",
    "    print(f\"Got exception trying to issue task: {str(e)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85d4b7c3-4d37-4042-895c-4675b9b3532b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Create a task against all active callbacks ################\n",
    "status = await mythic.issue_task_all_active_callbacks(\n",
    "        mythic=mythic_instance, command_name=\"shell\", parameters=\"whoami\"\n",
    "    )\n",
    "print(f\"Got the following list back: {status}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "595e8200-ade1-4821-a600-a162e9a9b0e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# ################ Create a task and wait for completion with timeout ################\n",
    "output = await mythic.issue_task_and_waitfor_task_output(\n",
    "        mythic=mythic_instance,\n",
    "        command_name=\"shell\",\n",
    "        parameters=\"whoami\",\n",
    "        callback_display_id=1,\n",
    "        timeout=10,\n",
    ")\n",
    "print(f\"Got the following output: {output}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bcdbb0f-6502-4a7a-ad50-1a435557d183",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Create a task and wait for output with timeout ################\n",
    "task = await mythic.issue_task(\n",
    "    mythic=mythic_instance,\n",
    "    command_name=\"shell\",\n",
    "    parameters=\"whoami\",\n",
    "    callback_display_id=2,\n",
    "    timeout=10,\n",
    "    wait_for_complete=True,\n",
    ")\n",
    "#### waitfor_for_task_output and issue_task_and_waitfor_task_output will fetch task and all subtask outputs\n",
    "output = await mythic.waitfor_for_task_output(\n",
    "    mythic=mythic_instance, task_display_id=task[\"display_id\"], timeout=10\n",
    ")\n",
    "print(f\"Got the following output the 2nd time: {output}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bc98085-7959-4c86-bace-e4adc4c2d3a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Get all output for a given task ################\n",
    "responses = await mythic.get_all_task_output_by_id(mythic=mythic_instance, task_display_id=14)\n",
    "print(responses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b605840-03d9-49c6-9f6f-20dcc4608a3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Get all tasks (optionally limited to a certain callback) ################\n",
    "resp = await mythic.get_all_tasks(mythic=mythic_instance, callback_display_id=6)\n",
    "print(resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a5cf644-9f5d-4ae3-a04a-104530773827",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ subscribe to new tasks on all callbacks or a single callback with timeout ################\n",
    "# only get up to 1 result for each new task\n",
    "async for result in mythic.subscribe_new_tasks(mythic=mythic_instance, callback_display_id=9, timeout=10):\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb3247a5-680f-44e6-b438-8d056148be23",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ subscribe to new tasks (and updates) on all callbacks or a single callback with timeout ################\n",
    "# get multiple results for each task as it goes through various processing stages\n",
    "async for result in mythic.subscribe_new_tasks_and_updates(mythic=mythic_instance, callback_display_id=9, timeout=30):\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2765b7c-0d0c-476f-ac06-9f5f6dcda360",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ get all old tasks and subscribe to all new tasks ################\n",
    "# only get up to 1 result for each task\n",
    "async for result in mythic.subscribe_all_tasks(mythic=mythic_instance, callback_display_id=9, timeout=30):\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5af19501-f6cc-442b-acdd-1bb165c78ded",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ get all old tasks and subscribe to all new tasks (and get their updates) ################\n",
    "# get multiple results for each task as it goes through various processing stages\n",
    "async for result in mythic.subscribe_all_tasks_and_updates(mythic=mythic_instance, callback_display_id=9, timeout=30):\n",
    "    print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3be3d37f-fb21-4e4a-aeda-8a2b3be245e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Add MITRE ATT&CK information to a task ################\n",
    "await mythic.add_mitre_attack_to_task(\n",
    "    mythic=mythic_instance, task_display_id=14, mitre_attack_numbers=[\"T1589\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47da8177-398a-4011-a451-d3cff866fd6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Get all task output for entire operation ################\n",
    "responses = await mythic.get_all_task_output(mythic=mythic_instance)\n",
    "print(responses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f95c55cf-4305-4f2b-9080-684cbd9c8beb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Get all task output a single task ################\n",
    "responses = await mythic.get_all_task_output_by_id(mythic=mythic_instance, task_display_id=25)\n",
    "print(responses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11b37f92-e469-4d0e-9547-f94147859246",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Get all task output a single task and all subtasks of that task ################\n",
    "responses = await mythic.get_all_task_and_subtask_output_by_id(mythic=mythic_instance, task_display_id=25)\n",
    "print(responses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c86664f2-2719-47d6-96d0-289363678019",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ################ Subscribe to new output across the entire operation ################\n",
    "async for response in mythic.subscribe_new_task_output(mythic=mythic_instance):\n",
    "        print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
